package cn.kgc.vue.controller;

import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Dept;
import cn.kgc.vue.service.DeptService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 课工场
 * @date 2023/12/7
 * @description
 */
@Api(tags = "部门模块")
@RestController
@RequestMapping("depts")
public class DeptController {

    @Resource
    private DeptService deptService;

    @ApiOperation("部门下拉选渲染接口")
    @GetMapping("select")
    public ResponseResult getDeptSelect(){
        List<Dept> list = deptService.list();
        return ResponseResult.success().data(list);
    }


    @ApiOperation("部门列表")
    @PostMapping("list")
    public ResponseResult getDeptList( @RequestBody  QueryCondition<Dept> queryCondition){

        return deptService.getDeptList(queryCondition);
    }

    //saveOrUpdate

    @ApiOperation("添加和修改部门信息")
    @PostMapping("saveOrUpdate")
    public ResponseResult saveOrUpdateDept(@RequestBody Dept dept){

        // 1.判定部门名称是否重复
        String deptName = dept.getDeptName();
        LambdaQueryWrapper<Dept> lambda = new QueryWrapper<Dept>().lambda();
        lambda.eq(Dept::getDeptName,deptName);

        Dept dep =deptService.getOne(lambda);
        if (ObjectUtils.isNotNull(dep)){
            return  ResponseResult.fail().message("部门名称已存在");
        }

        deptService.saveOrUpdate(dept);
        return ResponseResult.success();
    }


    @ApiOperation("根据id获取部门信息")
    @GetMapping("getDeptById/{deptId}")
    public ResponseResult getDeptSelect(@PathVariable Integer deptId){

        Dept dept = deptService.getById(deptId);
        return ResponseResult.success().data(dept);
    }


    @ApiOperation("根据id删除部门信息")
    @DeleteMapping("delete/{deptId}")
    public ResponseResult deleteDept(@PathVariable Integer deptId){

        return deptService.deleteDept(deptId);
    }

}
